<template>
  <table class="table">
    <thead class="table_header_title">
      <tr>
        <th>商品信息</th>
        <th>属性</th>
        <th>单价</th>
        <th>折后单价</th>
        <th>数量</th>
        <th>小计</th>
        <th>折后小计</th>
      </tr>
    </thead>

    <tbody class="table_tbody">
      <tr>
        <td class="noborder" colspan="7"></td>
      </tr>
      <tr class="goods" v-for="goods in list" :key="goods.orderGoodsId">
        <td>
          <div class="goods-info">
            <img class="goods-img" :src="goods.goodsImg" />
            <span class="goods-name"> {{ goods.goodsName }}</span>
          </div>
        </td>
        <td>
          <span>{{ goods.goodsSpec }}</span>
        </td>
        <td>
          <span v-if="goods.backGoodsPrice">¥{{ goods.backGoodsPrice.toFixed(2) }}</span>
          <span v-else>¥{{ goods.price.toFixed(2) }}</span>
        </td>
        <td>
          <span v-if="goods.backGoodsPrice">¥{{ goods.backDiscountPrice.toFixed(2) }}</span>
          <span v-else>¥{{ goods.discountPrice.toFixed(2) }}</span>
        </td>
        <td class="amount">
          <span>{{ goods.amount }}</span>
        </td>
        <td class="goods-price last">
          <span v-if="type === 'afterSale'">¥{{ (goods.backGoodsPrice * (goods.amount || 1)).toFixed(2) }}</span>
          <span v-else>¥{{ (goods.amount * goods.price).toFixed(2) }}</span>
        </td>
        <td class="goods-price last">
          <span v-if="type === 'afterSale'">¥{{ (goods.backDiscountPrice * (goods.amount || 1)).toFixed(2) }}</span>
          <span v-else>¥{{ (goods.amount * goods.discountPrice).toFixed(2) }}</span>
        </td>
      </tr>

      <tr class="sum" v-if="type === 'afterSale' && afterSaleFlag === 'COMPLETED'">
        <td class="last" colspan="7">合计：¥{{ leftPrice }}</td>
      </tr>
      <tr class="sum" v-if="type === 'goods'">
        <td class="last" colspan="7">合计： ¥{{ orderPrice }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script setup>
import { computed, reactive, toRefs } from 'vue'
const props = defineProps(['list', 'type', 'afterSaleFlag', 'orderPrice'])

const { list } = toRefs(props)

const tableData = reactive({
  list: list
})

const leftPrice = computed(() => {
  let sum = tableData.list.map((e) => e.amount * e.backDiscountPrice).reduce((acc, cur) => cur + acc, 0)
  return sum.toFixed(2)
})
</script>
<style scoped lang="scss">
.table {
  border-collapse: collapse;
  width: calc(100% - 104px);
  margin-top: 20px;

  th {
    width: 160px;
  }
  .table_header_title {
    height: 42px;
    background-color: #f9f9f9;
    color: #909399;

    margin-bottom: 20px;
  }

  .table_tbody {
    td.noborder {
      border: 0;
      height: 20px;
    }

    td.noborder:first-child {
      border-left: none;
    }

    tr {
      td {
        line-height: 18px;
        text-align: center;
      }

      td:first-child {
        border-left: 1px solid var(--border-color);
      }

      td.last {
        border-right: 1px solid var(--border-color);
      }

      td.border {
        border-bottom: 1px solid var(--border-color);
        padding: 10px 35px 13px 54px;
        .remark {
          display: flex;
          justify-content: space-between;

          .title {
            font-weight: 500;
            line-height: 16px;
          }

          .price {
            font-weight: 600;
            line-height: 18px;
            color: var(--color-primary);
          }
        }
      }

      td.amount {
        border-left: 1px solid var(--border-color);
        border-right: 1px solid var(--border-color);
      }

      td.goods-price {
        font-weight: 600;
        color: var(--color-primary);
      }
    }

    .goods,
    .sum {
      td {
        border-bottom: 1px solid var(--border-color);
        border-top: 1px solid var(--border-color);
        padding: 20px 0;
      }
    }

    .goods {
      td {
        .goods-info {
          display: flex;
          align-items: center;
          padding-left: 54px;

          .goods-img {
            display: inline-block;
            width: 72px;
            height: 72px;
            margin-right: 12px;
          }

          .goods-name {
            color: var(--color-text);
            width: 180px;
            line-height: 18px;
          }
        }
      }
    }

    .sum {
      td {
        color: red;
        font-weight: 600;
        font-size: 16px;
      }
      td:last-child {
        text-align: right;
        padding-right: 30px;
      }
    }
  }
}
</style>
